#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   NRC22_Phy_test.py
@Time    :   2022/07/06 13:31:28
@Author  :   YangLiang 
@Version :   1.0
@Contact :   YangLiang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from pytest_assume.plugin import assume

import pytest
import logging
import time
import Set_up
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True

SID31_RIDlist_App = [
    # ([0x31,0x01,0x02,0x03],[0x7F,0x31,0x22]),
    ([0x31, 0x01, 0x12, 0x7A], [0x7F, 0x31, 0x22]),
    ([0x31, 0x02, 0x12, 0x7A], [0x7F, 0x31, 0x22]),
    ([0x31, 0x03, 0x12, 0x7A], [0x7F, 0x31, 0x22])]
# ([0x31,0x01,0x12,0x7B],[0x7F,0x31,0x22]),
# ([0x31,0x02,0x12,0x7B],[0x7F,0x31,0x22]),
# ([0x31,0x03,0x12,0x7B],[0x7F,0x31,0x22])]

# SID28 = [([0x28,0x03,0x01],[0x7F,0x28,0x22]),
#         ([0x28,0x03,0x02],[0x7F,0x28,0x22]),
#         ([0x28,0x03,0x03],[0x7F,0x28,0x22]),
#         ([0x28,0x02,0x01],[0x7F,0x28,0x22]),
#         ([0x28,0x02,0x02],[0x7F,0x28,0x22]),
#         ([0x28,0x02,0x03],[0x7F,0x28,0x22]),
#         ([0x28,0x01,0x01],[0x7F,0x28,0x22]),
#         ([0x28,0x01,0x02],[0x7F,0x28,0x22]),
#         ([0x28,0x01,0x03],[0x7F,0x28,0x22]),
#         ([0x28,0x00,0x01],[0x7F,0x28,0x22]),
#         ([0x28,0x00,0x02],[0x7F,0x28,0x22]),
#         ([0x28,0x00,0x03],[0x7F,0x28,0x22])]

# WriteDID_App = [([0x2E,0xF1,0x90,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfE],[0x7F,0x2E,0x22]),
#                 ([0x2E,0x12,0x66,0x00,0x00,0x00,0x01],[0x7F,0x2E,0x22]),
#                 ([0x2E,0x12,0x6A,0x00,0x01],[0x7F,0x2E,0x22]),
#                 ([0x2E,0x12,0x6B,0x00,0x01],[0x7F,0x2E,0x22]),
#                 ([0x2E,0x12,0x6C,0x00,0x01],[0x7F,0x2E,0x22])]

# WriteDID_Boot = [([0x2E,0xF1,0x84,0x22,0x09,0x30,0x00,0x00,0x00,0x00,0x00,0x01],[0x7F,0x2E,0x22]),]

Default_mode = [
    ([0x14, 0xFF, 0xFF, 0xFF], [0x7F, 0x14, 0x22]),
    ([0x11, 0x01], [0x7F, 0x11, 0x22]),
    ([0x11, 0x03], [0x7F, 0x11, 0x22])]


Extended_mode = [   ([0x10, 0x02], [0x7F, 0x10, 0x22]),
                    ([0x14, 0xFF, 0xFF, 0xFF], [0x7F, 0x14, 0x22]),
                    ([0x11, 0x01], [0x7F, 0x11, 0x22]),
                    ([0x11, 0x03], [0x7F, 0x11, 0x22]),
                    ([0x28, 0x03, 0x02], [0x7F, 0x28, 0x22]),
                    ([0x28, 0x02, 0x02], [0x7F, 0x28, 0x22]),
                    ([0x28, 0x01, 0x01], [0x7F, 0x28, 0x22]),
                    ([0x28, 0x01, 0x02], [0x7F, 0x28, 0x22]),
                    ([0x28, 0x01, 0x03], [0x7F, 0x28, 0x22]),
                    ([0x28, 0x00, 0x01], [0x7F, 0x28, 0x22]),
                    ([0x28, 0x00, 0x02], [0x7F, 0x28, 0x22]),
                    ([0x28, 0x00, 0x03], [0x7F, 0x28, 0x22]),
                    # ([0x85, 0x02], [0x7F, 0x85, 0x22]),   诊断调查表里0x85没有写NRC22条件，但支持的NRC有22存在，所以该如何判定?
                    # ([0x85, 0x01], [0x7F, 0x85, 0x22]),   诊断调查表里0x85没有写NRC22条件，但支持的NRC有22存在，所以该如何判定?
                    ([0x31, 0x01, 0x02, 0x03], [0x7F, 0x31, 0x22]),
                    ([0x31, 0x01, 0x12, 0x7A], [0x7F, 0x31, 0x22]),
                    ([0x31, 0x02, 0x12, 0x7A], [0x7F, 0x31, 0x22]),
                    ([0x31, 0x03, 0x12, 0x7A], [0x7F, 0x31, 0x22]),
                    ([0x31, 0x01, 0x12, 0x7B], [0x7F, 0x31, 0x22]),
                    ([0x2E, 0xF1, 0x90, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfE], [0x7F, 0x2E, 0x22]),
                    ([0x2E, 0x12, 0x66, 0x00, 0x00, 0x00, 0x01], [0x7F, 0x2E,0x22]),
                    ([0x2E, 0x12, 0x6A, 0x00, 0x01], [0x7F, 0x2E, 0x22]),
                    ([0x2E, 0x12, 0x6B, 0x00, 0x01], [0x7F, 0x2E, 0x22]),
                    ([0x2E, 0x12, 0x6C, 0x00, 0x01], [0x7F, 0x2E, 0x22]),
                ]


Extended_unlock = [([0x31, 0x01, 0x02, 0x03], [0x7F, 0x31, 0x22]),
                   ([0x31, 0x01, 0x12, 0x7A], [0x7F, 0x31, 0x22]),
                   ([0x31, 0x02, 0x12, 0x7A], [0x7F, 0x31, 0x22]),
                   ([0x31, 0x03, 0x12, 0x7A], [0x7F, 0x31, 0x22]),
                   ([0x31, 0x01, 0x12, 0x7B], [0x7F, 0x31, 0x22]),
                   ([0x31, 0x02, 0x12, 0x7B], [0x7F, 0x31, 0x22]),
                   ([0x31, 0x03, 0x12, 0x7B], [0x7F, 0x31, 0x22]),
                   ([0x2E, 0xF1, 0x90, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
                    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfE], [0x7F, 0x2E, 0x22]),
                   ([0x2E, 0x12, 0x66, 0x00, 0x00, 0x00, 0x01], [0x7F, 0x2E, 0x22]),
                   ([0x2E, 0x12, 0x6A, 0x00, 0x01], [0x7F, 0x2E, 0x22]),
                   ([0x2E, 0x12, 0x6B, 0x00, 0x01], [0x7F, 0x2E, 0x22]),
                   ([0x2E, 0x12, 0x6C, 0x00, 0x01], [0x7F, 0x2E, 0x22]),
                   ]

Overspeed_Extended_unlock = [([0x31, 0x01, 0x02, 0x03], [0x7F, 0x31, 0x22]),
                             ([0x31, 0x01, 0x12, 0x7A], [0x7F, 0x31, 0x22]),
                             ([0x31, 0x03, 0x12, 0x7A], [0x7F, 0x31, 0x22]),
                             ([0x2E, 0xF1, 0x90, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
                              0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfE], [0x7F, 0x2E, 0x22]),
                             ([0x2E, 0x12, 0x66, 0x00, 0x00,0x00, 0x01], [0x7F, 0x2E, 0x22]),
                             ([0x2E, 0x12, 0x6A, 0x00, 0x01],[0x7F, 0x2E, 0x22]),
                             ([0x2E, 0x12, 0x6B, 0x00, 0x01],[0x7F, 0x2E, 0x22]),
                             ([0x2E, 0x12, 0x6C, 0x00, 0x01],[0x7F, 0x2E, 0x22]),
                             ]

Programming_mode = [([0x11, 0x01], [0x7F, 0x11, 0x22]),
                    ([0x11, 0x03], [0x7F, 0x11, 0x22]),
                    ([0x2E, 0xF1, 0x84, 0x22, 0x09, 0x30, 0x00, 0x00,0x00, 0x00, 0x00, 0x01], [0x7F, 0x2E, 0x22]),
                ]


'''
    SpeedVD_signal 0x0: not valid
    SpeedVD_signal 0x1: valid
    VCU_STRATEGY_SHIFT_POSITION 0x01:p
    VCU_STRATEGY_SHIFT_POSITION 0x02:R
    VCU_STRATEGY_SHIFT_POSITION 0x03:N
    VCU_STRATEGY_SHIFT_POSITION 0x04:D
    VCU_STRATEGY_SHIFT_POSITION 0x11:M
    VCU_STRATEGY_SHIFT_POSITION 0x12:S
    VCU_STRATEGY_SHIFT_POSITION 0x15:undifind

'''
SpeedVD_signal = Set_up.NetName, Set_up.nodeName, 'PBM_0x185', 'VehSpdVld'
SpeedACT_signal = Set_up.NetName, Set_up.nodeName, 'PBM_0x185', 'VehSpd'
N_ShiftLeverPos = Set_up.NetName, Set_up.nodeName, 'VCU_PDCU_0x230', 'VCU_STRATEGY_SHIFT_POSITION'


@allure.severity('critical')
class Test_NRC22_Phy(object):

    def setSignalValue(self, Driver, signal_list, value):
        Driver['CAN'].write_db_signal(signal_list[0], signal_list[1], signal_list[2], signal_list[3], value)
        time.sleep(0.2)
        readValue = Driver['CAN'].read_db_signal(signal_list[0], signal_list[1], signal_list[2], signal_list[3])


    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in Default_mode]
    @allure.story('ECU NRC 22 Test')
    @allure.description('Test Service without unlock Undervoltage confition response NRC22 in default session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', Default_mode, ids=ids)
    def test_Undervoltage_Default_Phy(self, action, Driver, req, resp):
        iStep = 1
        correctResponse = [req[0] + 0x40]
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 1, iStep)
        time.sleep(1)
        iStep = Driver['CAN'].SetVolt(Driver, 8, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 12, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 0, iStep)

    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in Default_mode]
    @allure.description('Test Service without unlock Undervoltage confition response NRC22 in default session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', Default_mode, ids=ids)
    def test_Overvoltage_Default_Phy(self, action, Driver, req, resp):
        iStep = 1
        correctResponse = [req[0] + 0x40]
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 1, iStep)
        time.sleep(1)
        iStep = Driver['CAN'].SetVolt(Driver, 17, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 12, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 0, iStep)

    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in Default_mode]
    @allure.description('Test Service without unlock Vspeed over 3kph confition response NRC22 in default session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', Default_mode, ids=ids)
    def test_OverVspeed_Default_Phy(self, action, Driver, req, resp):
        iStep = 1
        correctResponse = [req[0] + 0x40]
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 1, iStep)
        time.sleep(1)
        with allure.step(f'Step{iStep}:Request:设置车速为4kph'):
            self.setSignalValue(Driver, SpeedVD_signal, 1)
            self.setSignalValue(Driver, SpeedACT_signal, 4)
            logging.info('wait 0.5s')
            time.sleep(0.5)
            iStep += 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep)
        with allure.step(f'Step{iStep}:Request:设置车速为0kph,并等待1s'):
            self.setSignalValue(Driver, SpeedACT_signal, 0)
            logging.info('wait 0.5s')
            time.sleep(0.5)
            iStep += 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 0, iStep)

    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in Extended_mode]
    @allure.story('ECU NRC 22 Test')
    @allure.description('Test Service without unlock Undervoltage confition response NRC22 in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', Extended_mode, ids=ids)
    def test_Undervoltage_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        time.sleep(0.5)
        if req == [0x2E, 0xF1, 0x84, 0x22, 0x09, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01] or req == [0x31, 0x01, 0x02, 0x02]:
            correctResponse = resp
        elif req[0:2] == [0x31, 0x02]:
            correctResponse = [0x7F, 0x31, 0x24]
        else:
            correctResponse = [req[0] + 0x40]
        if req[0] == 0x10:
            iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31 01 02 03', '71 01 02 03', 3, iStep)
            time.sleep(1)
            iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 3, iStep)
        elif req[0] == 0x11:
            iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 3, iStep, True)
            time.sleep(2)
            iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 3, iStep)
        else:
            iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 3, iStep, True)
    
        iStep = Driver['CAN'].SetVolt(Driver, 8, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 12, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 0, iStep)
        if req[0:3] == [0x31, 0x01, 0x12]:
            req[1] = 0x02           
            iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 0, iStep)

    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in Extended_mode]
    @allure.description('Test Service without unlock Undervoltage confition response NRC22 in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', Extended_mode, ids=ids)
    def test_Overvoltage_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        time.sleep(0.5)
        if req == [0x2E, 0xF1, 0x84, 0x22, 0x09, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01] or req == [0x31, 0x01, 0x02, 0x02]:
            correctResponse = resp
        elif req[0:2] == [0x31, 0x02] or req[0:2] == [0x31, 0x03]:
            correctResponse = [0x7F, 0x31, 0x24]
        else:
            correctResponse = [req[0] + 0x40]
        if req[0] == 0x10:
            iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31 01 02 03', '71 01 02 03', 3, iStep)
        elif req[0] == 0x11:
            iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 3, iStep, True)
            time.sleep(2)
            iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 3, iStep)
        else:
            iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 3, iStep, True)
        iStep = Driver['CAN'].SetVolt(Driver, 17, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 12, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 0, iStep)
        if req[0:3] == [0x31, 0x01, 0x12]:
            req[1] = 0x02  
            iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 0, iStep)

    ids = ["Request {}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in Extended_mode]
    @allure.description('Test Service without unlock Vspeed over 3kph confition response NRC22 in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', Extended_mode, ids=ids)
    def test_OverVspeed_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        time.sleep(0.5)
        if req == [0x2E, 0xF1, 0x84, 0x22, 0x09, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01] or req == [0x31, 0x01, 0x02, 0x02]:
            correctResponse = resp
        elif req[0:2] == [0x31, 0x02] or req[0:2] == [0x31, 0x03]:  #250115 增加3103判断
            correctResponse = [0x7F, 0x31, 0x24]
        else:
            correctResponse = [req[0] + 0x40]
        if req[2:4] == [0x12, 0x7B]:
            resp = [0x71]
        if req[0:2] == [0x10,0x02]:
            iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31 01 02 03', '71 01 02 03', 3, iStep, False)
        elif req[0] == [0x11]:
            iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 3, iStep, False)
            time.sleep(2)
            iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 3, iStep, False)
        else:
            iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 3, iStep, True)
        with allure.step(f'Step{iStep}:Request:设置车速为4kph'):
            self.setSignalValue(Driver, SpeedVD_signal, 1)
            self.setSignalValue(Driver, SpeedACT_signal, 4)
            logging.info('wait 0.5s')
            time.sleep(0.5)
            iStep += 1
        if req == [0x28, 0x02, 0x01] or req == [0x28, 0x02, 0x03] or req == [0x28, 0x03, 0x01] or req == [0x28, 0x03, 0x03]:
            resp = correctResponse
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep)
        with allure.step(f'Step{iStep}:Request:设置车速为0kph,并等待1s'):
            self.setSignalValue(Driver, SpeedACT_signal, 0)
            logging.info('wait 0.5s')
            time.sleep(0.5)
            iStep += 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 0, iStep)
        if req[0] == 0x11:
            time.sleep(1)
        
        if req[0:3] == [0x31, 0x01, 0x12]:
            req[1] = 0x02
            iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 0, iStep)

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in Extended_unlock]
    @allure.description('Test Service with unlock Undervoltage confition response NRC22 in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', Extended_unlock, ids=ids)
    def test_Undervoltage_unlock_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        time.sleep(0.5)
        correctResponse = [req[0] + 0x40]
        if req == [0x2E, 0xF1, 0x84, 0x22, 0x09, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01] or req == [0x31, 0x01, 0x02, 0x02]:
            correctResponse = resp
        if req[0:2] == [0x31, 0x02]:
            correctResponse = [0x7F, 0x31, 0x24]
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 3, iStep, True)
        iStep = Driver['CAN'].SetVolt(Driver, 8, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 12, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 0, iStep)

        if req[0:3] == [0x31, 0x01, 0x12]:
            req[1] = 0x02  
            
            iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 0, iStep)

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in Extended_unlock]
    @allure.description('Test Service with unlock Overvoltage confition response NRC22 in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', Extended_unlock, ids=ids)
    def test_Overvoltage_unlock_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        time.sleep(0.5)
        correctResponse = [req[0] + 0x40]
        if req == [0x2E, 0xF1, 0x84, 0x22, 0x09, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01] or req == [0x31, 0x01, 0x02, 0x02]:
            correctResponse = resp
        if req[0:2] == [0x31, 0x02]:
            correctResponse = [0x7F, 0x31, 0x24]
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 3, iStep, True)
        iStep = Driver['CAN'].SetVolt(Driver, 17, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 12, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 0, iStep)
        if req[0:3] == [0x31, 0x01, 0x12]:
            req[1] = 0x02  
            
            iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 0, iStep)

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in Overspeed_Extended_unlock]
    @allure.description('Test Service with unlock Vspeed over 3kph confition response NRC22 in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', Overspeed_Extended_unlock, ids=ids)
    def test_OverVspeed_unlock_Extended_Phy(self, action, Driver, req, resp):
        iStep = 1
        correctResponse = [req[0] + 0x40]
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 3, iStep, True)
        with allure.step(f'Step{iStep}:Request:设置车速为4kph'):
            self.setSignalValue(Driver, SpeedVD_signal, 1)
            self.setSignalValue(Driver, SpeedACT_signal, 4)
            logging.info('wait 0.5s')
            time.sleep(0.5)
            iStep += 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep)
        with allure.step(f'Step{iStep}:Request:设置车速为0kph,并等待1s'):
            self.setSignalValue(Driver, SpeedACT_signal, 0)
            logging.info('wait 0.5s')
            time.sleep(0.5)
            iStep += 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 0, iStep)

    # boot 下电压固定12V
    # ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in Programming_mode]
    # @allure.description('Test Service Undervoltage confition response NRC22 in programming session with physical addressing')
    # @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    # @pytest.mark.parametrize('req,resp', Programming_mode, ids=ids)
    # def test_Undervoltage_programming_Phy(self, action, Driver, req, resp):
    #     iStep = 1
    #     correctResponse = [req[0] + 0x40]
    #     if req[0] == 0x11:
    #         iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 2, iStep, False)
    #         time.sleep(1)
    #         iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 2, iStep, False)
    #     else:
    #         iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 2, iStep, True)
    #     iStep = Driver['CAN'].SetVolt(Driver, 8, 1, iStep)
    #     iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)
    #     iStep = Driver['CAN'].SetVolt(Driver, 12, 1, iStep)
    #     iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 0, iStep)

    # # Boot下电压固定12V
    # ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in Programming_mode]
    # @allure.description('Test Service Overvoltage confition response NRC22 in programming session with physical addressing')
    # @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    # @pytest.mark.parametrize('req,resp', Programming_mode, ids=ids)
    # def test_Overvoltage_programming_Phy(self, action, Driver, req, resp):
    #     iStep = 1
    #     correctResponse = [req[0] + 0x40]
      
    #     if req[0] == 0X11:
    #         iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 2, iStep, False)
    #         time.sleep(1)
    #         iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 2, iStep, False)
    #     else:
    #         iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 2, iStep, True)
    #     iStep = Driver['CAN'].SetVolt(Driver, 17, 1, iStep)
    #     iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)
    #     iStep = Driver['CAN'].SetVolt(Driver, 12, 1, iStep)
    #     iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 0, iStep)

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in Programming_mode]
    @allure.description('Test Service with unlock Vspeed over 3kph confition response NRC22 in programming session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', Programming_mode, ids=ids)
    def test_OverVspeed_programming_Phy(self, action, Driver, req, resp):
        iStep = 1
        time.sleep(0.5)
        correctResponse = [req[0] + 0x40]
        if req[0] == 0X11:
            iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 2, iStep, False)
            time.sleep(1)
            iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 2, iStep, False)
        else:
            iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 2, iStep, True)
        with allure.step(f'Step{iStep}:Request:设置车速为4kph'):
            self.setSignalValue(Driver, SpeedVD_signal, 1)
            self.setSignalValue(Driver, SpeedACT_signal, 4)
            logging.info('wait 0.5s')
            time.sleep(0.5)
            iStep += 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)
        with allure.step('Step5:Request:设置车速为0kph'):
            self.setSignalValue(Driver, SpeedACT_signal, 0)
            self.setSignalValue(Driver, SpeedVD_signal, 0)
            logging.info('wait 0.5s')
            time.sleep(0.5)
            iStep += 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, correctResponse, 0, iStep, False)

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in SID31_RIDlist_App]
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', SID31_RIDlist_App, ids=ids)
    def test_SID31_0x3e0_VCU_STRATEGY_SHIFT_POSITION_0x03_gearR_Extended_Phy(self, action, Driver, req, resp):
        '''
        VCU_STRATEGY_SHIFT_POSITION 0x01:p
        VCU_STRATEGY_SHIFT_POSITION 0x02:R
        VCU_STRATEGY_SHIFT_POSITION 0x03:N
        VCU_STRATEGY_SHIFT_POSITION 0x04:D
        VCU_STRATEGY_SHIFT_POSITION 0x11:M
        VCU_STRATEGY_SHIFT_POSITION 0x12:S
        VCU_STRATEGY_SHIFT_POSITION 0x15:undifind
        '''
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 3, iStep, True)
        with allure.step(f'Step{iStep}:Request:设置信{N_ShiftLeverPos}为2'):
            self.setSignalValue(Driver, N_ShiftLeverPos, 3)
            time.sleep(0.5)
            iStep += 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)
        with allure.step(f'Step5:Request:设置信号{N_ShiftLeverPos}为1'):
            self.setSignalValue(Driver, N_ShiftLeverPos, 1)
            time.sleep(0.5)

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in SID31_RIDlist_App]
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', SID31_RIDlist_App, ids=ids)
    def test_SID31_0x3e0_VCU_STRATEGY_SHIFT_POSITION_0x04_gearD_Extended_Phy(self, action, Driver, extend_session, req, resp):
        '''
        VCU_STRATEGY_SHIFT_POSITION 0x01:p
        VCU_STRATEGY_SHIFT_POSITION 0x02:R
        VCU_STRATEGY_SHIFT_POSITION 0x03:N
        VCU_STRATEGY_SHIFT_POSITION 0x04:D
        VCU_STRATEGY_SHIFT_POSITION 0x11:M
        VCU_STRATEGY_SHIFT_POSITION 0x12:S
        VCU_STRATEGY_SHIFT_POSITION 0x15:undifind
        '''

        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 3, iStep, True)
        with allure.step(f'Step{iStep}:Request:设置信{N_ShiftLeverPos}为4'):
            self.setSignalValue(Driver, N_ShiftLeverPos, 4)
            time.sleep(0.5)
            iStep += 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)
        with allure.step(f'Step5:Request:设置信号{N_ShiftLeverPos}为1'):
            self.setSignalValue(Driver, N_ShiftLeverPos, 1)
            time.sleep(0.5)

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))
           for req, resp in SID31_RIDlist_App]

    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', SID31_RIDlist_App, ids=ids)
    def test_SID31_0x3e0_VCU_STRATEGY_SHIFT_POSITION_0x11_gearM_Extended_Phy(self, action, Driver, extend_session, req, resp):
        '''
        VCU_STRATEGY_SHIFT_POSITION 0x01:p
        VCU_STRATEGY_SHIFT_POSITION 0x02:R
        VCU_STRATEGY_SHIFT_POSITION 0x03:N
        VCU_STRATEGY_SHIFT_POSITION 0x04:D
        VCU_STRATEGY_SHIFT_POSITION 0x11:M
        VCU_STRATEGY_SHIFT_POSITION 0x12:S
        VCU_STRATEGY_SHIFT_POSITION 0x15:undifind
        '''

        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 3, iStep, True)
        with allure.step(f'Step{iStep}:Request:设置信{N_ShiftLeverPos}为11'):
            self.setSignalValue(Driver, N_ShiftLeverPos, 11)
            time.sleep(0.5)
            iStep += 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)
        with allure.step(f'Step5:Request:设置信号{N_ShiftLeverPos}为1'):
            self.setSignalValue(Driver, N_ShiftLeverPos, 1)
            time.sleep(0.5)

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))
           for req, resp in SID31_RIDlist_App]

    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', SID31_RIDlist_App, ids=ids)
    def test_SID31_0x3e0_VCU_STRATEGY_SHIFT_POSITION_0x12_gearS_Extended_Phy(self, action, Driver, extend_session, req, resp):
        '''
        VCU_STRATEGY_SHIFT_POSITION 0x01:p
        VCU_STRATEGY_SHIFT_POSITION 0x02:R
        VCU_STRATEGY_SHIFT_POSITION 0x03:N
        VCU_STRATEGY_SHIFT_POSITION 0x04:D
        VCU_STRATEGY_SHIFT_POSITION 0x11:M
        VCU_STRATEGY_SHIFT_POSITION 0x12:S
        VCU_STRATEGY_SHIFT_POSITION 0x15:undifind
        '''

        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 3, iStep, True)
        with allure.step(f'Step{iStep}:Request:设置信{N_ShiftLeverPos}为12'):
            self.setSignalValue(Driver, N_ShiftLeverPos, 12)
            time.sleep(0.5)
            iStep += 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)
        with allure.step(f'Step5:Request:设置信号{N_ShiftLeverPos}为1'):
            self.setSignalValue(Driver, N_ShiftLeverPos, 1)
            time.sleep(0.5)

        with allure.step(f'Step5:Request:设置信号{N_ShiftLeverPos}为1'):
            self.setSignalValue(Driver, N_ShiftLeverPos, 1)
            time.sleep(1)

    ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req), ' '))
           for req, resp in SID31_RIDlist_App]

    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', SID31_RIDlist_App, ids=ids)
    def test_SID31_0x3e0_VCU_STRATEGY_SHIFT_POSITION_0x15_undifind_Extended_Phy(self, action, Driver, extend_session, req, resp):
        '''
        VCU_STRATEGY_SHIFT_POSITION 0x01:p
        VCU_STRATEGY_SHIFT_POSITION 0x02:R
        VCU_STRATEGY_SHIFT_POSITION 0x03:N
        VCU_STRATEGY_SHIFT_POSITION 0x04:D
        VCU_STRATEGY_SHIFT_POSITION 0x11:M
        VCU_STRATEGY_SHIFT_POSITION 0x12:S
        VCU_STRATEGY_SHIFT_POSITION 0x15:undifind
        '''
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E 00', '7E 00', 3, iStep, True)
        with allure.step(f'Step{iStep}:Request:设置信{N_ShiftLeverPos}为15'):
            self.setSignalValue(Driver, N_ShiftLeverPos, 15)
            time.sleep(0.5)
            iStep += 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 0, iStep, False)
        with allure.step(f'Step5:Request:设置信号{N_ShiftLeverPos}为1'):
            self.setSignalValue(Driver, N_ShiftLeverPos, 1)
            time.sleep(0.5)

    @allure.description('Test 0x10 Service undervoltage boundary confition response NRC22 in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_SID10_undervoltage_boundary_Extended_Phy(self, action, Driver):
        iStep = 1
        iStep = Driver['CAN'].SetVolt(Driver, 9.4, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E 80', '', 3, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31 01 02 03', '71 01 02 03', 0, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 02', '50 02', 0, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 9.2, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 03', '50 03', 1, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31 01 02 03', '71 01 02 03', 0, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 02', '50 02', 0, iStep, False)

        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 03', '50 03', 1, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31 01 02 03', '71 01 02 03', 0, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 8.8, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 02', '7F 10 22', 0, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 9.2, 1, iStep)

        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 03', '50 03', 1, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31 01 02 03', '71 01 02 03', 0, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 8.5, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 02', '7F 10 22', 0, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 12, 1, iStep)
        
        

    @allure.description('Test 0x10 Service 0vervoltage boundary confition response NRC22 in extended session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_SID10_overvoltage_boundary_Extended_Phy(self, action, Driver):

        iStep = 1
        iStep = Driver['CAN'].SetVolt(Driver, 15.7, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '3E 80', '', 3, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31 01 02 03', '71 01 02 03', 0, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 02', '50 02', 0, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 15.8, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 03', '50 03', 1, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31 01 02 03', '71 01 02 03', 0, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 02', '50 02', 0, iStep, False)

        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 03', '50 03', 1, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31 01 02 03', '71 01 02 03', 0, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 16.2, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 02', '7F 10 22', 0, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 15.6, 1, iStep)

        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 03', '50 03', 1, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31 01 02 03', '71 01 02 03', 0, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 16.5, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 02', '7F 10 22', 0, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 15.5, 1, iStep)

        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 03', '50 03', 1, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31 01 02 03', '71 01 02 03', 0, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 16.4, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 02', '7F 10 22', 0, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 15.75, 1, iStep)

        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 03', '50 03', 1, iStep, False)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '31 01 02 03', '71 01 02 03', 0, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 16.25, 1, iStep)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, '10 02', '7F 10 22', 0, iStep, False)
        iStep = Driver['CAN'].SetVolt(Driver, 12, 1, iStep)
